//SPDX-FileCopyrightText: Copyright 2022-2025 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/alarm_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/api_coll_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/appstore_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/bulletin_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/client_cfg_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/code_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/comment_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/dataview_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/event_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/fs_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/idea_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/issue_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/mail_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/org_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/org_eval_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_doc_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_draw_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_entry_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_review_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/requirement_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/roadmap_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/short_note_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/sw_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/testcase_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/triger_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/user_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/watch_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/widget_dao"
	"github.com/redis/go-redis/v9"
	"go.mongodb.org/mongo-driver/mongo"
)

var _mongoClient *mongo.Client

var CacheDao *_CacheDao
var KeywordDao *_KeywordDao
var BgTaskDao *_BgTaskDao
var MsgDataDao *_MsgDataDao
var VersionDao *_VersionDao
var AdminUserDao *_AdminUserDao
var FeedBackDao *_FeedBackDao

func InitDao(mgoDb *mongo.Database, rdb *redis.Client) (err error) {
	_mongoClient = mgoDb.Client()
	CacheDao, err = newCacheDao(rdb)
	if err != nil {
		return
	}
	KeywordDao, err = newKeywordDao(mgoDb)
	if err != nil {
		return
	}
	BgTaskDao, err = newBgTaskDao(mgoDb)
	if err != nil {
		return
	}
	MsgDataDao, err = newMsgDataDao(mgoDb)
	if err != nil {
		return
	}
	VersionDao, err = newVersionDao(mgoDb)
	if err != nil {
		return
	}
	AdminUserDao, err = newAdminUserDao(mgoDb)
	if err != nil {
		return
	}
	FeedBackDao, err = newFeedBackDao(mgoDb)
	if err != nil {
		return
	}

	//=========================================其他dao初始化
	err = issue_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = project_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = event_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = user_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = fs_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = project_doc_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	err = project_draw_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = client_cfg_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = short_note_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = requirement_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = appstore_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = code_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = idea_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = alarm_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = bulletin_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = api_coll_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = project_entry_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = watch_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = comment_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = testcase_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = org_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = org_eval_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = widget_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = sw_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = dataview_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = mail_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = roadmap_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = project_review_dao.InitDao(mgoDb)
	if err != nil {
		return
	}

	err = triger_dao.InitDao(mgoDb)
	if err != nil {
		return
	}
	return
}

func StartSession() (mongo.Session, error) {
	return _mongoClient.StartSession()
}

func EndSession(sess mongo.Session) {
	sess.EndSession(context.Background())
}
